package io.framework.modules.air.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.common.utils.DateUtils;
import io.framework.modules.air.dao.*;
import io.framework.modules.air.entity.AirCarAutoDataEntity;
import io.framework.modules.air.entity.AirCarStatic;
import io.framework.modules.air.service.AirCarAutoDataService;
import io.framework.modules.water.entity.WaterAutoDataEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author Gujq email
 * @since 1.0.0 2020-05-20
 */
@Service
public class AirCarAutoDataServiceImpl extends CrudServiceImpl<AirCarAutoDataDao, AirCarAutoDataEntity, AirCarAutoDataEntity> implements AirCarAutoDataService {

    @Autowired
    private AirCarAutoDataDao airCarAutoDataDao;

    @Override
    public QueryWrapper<AirCarAutoDataEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    @Override
    public PageData<AirCarAutoDataEntity> getAirCarAutoListByParams(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));;
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        Page<WaterAutoDataEntity> p = new Page<>(page, limit);
        IPage<AirCarAutoDataEntity> ip = baseDao.getAirCarAutoListByParams(p,params);
        List<AirCarAutoDataEntity> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), AirCarAutoDataEntity.class);

        return new PageData<AirCarAutoDataEntity>(dtoList, ip.getTotal());
    }

    @Override
    public List<Map<String, Object>> getAirCarAutoDataMonthChange(Map<String, Object> params) {
        if (params.get("startDate")==null) {
            Integer dateType = Integer.parseInt(params.get("dateType").toString());
            String date = params.get("date").toString();
            String endDate = "", startDate = "";
            int day = 0;
            if (dateType == 1) {
                //年
                date = DateUtils.format(DateUtils.parse(date, "yyyy"), "yyyy");
                startDate = date + "-01-01";
                endDate = date + "-12-31";
            } else if (dateType == 2) {
                int yyyy = Integer.parseInt(date.substring(0, date.indexOf("-")));
                int q = Integer.parseInt(date.substring(date.indexOf("Q") + 1, date.length()));
                if (q == 1) {
                    startDate = yyyy + "-01-01";
                    endDate = yyyy + "-03-31";
                } else if (q == 2) {
                    startDate = yyyy + "-04-01";
                    endDate = yyyy + "-06-31";
                } else if (q == 3) {
                    startDate = yyyy + "-07-01";
                    endDate = yyyy + "-09-31";
                } else if (q == 4) {
                    startDate = yyyy + "-10-01";
                    endDate = yyyy + "-12-31";
                }
            } else if (dateType == 3) {
                date = date + "-01";
                //一个月有多少天
                day = DateUtils.getDaysOfMonth(DateUtils.parse(date, DateUtils.DATE_MONTH_PATTERN), Calendar.DAY_OF_MONTH);
                startDate = DateUtils.getMonthBegin(date);
                endDate = DateUtils.getMonthEnd(date);
            }
            params.put("startDate", startDate);
            params.put("endDate", endDate);
        }
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        String stationName[] = params.get("stationName").toString().replaceAll(" ", "").split(",");
        for (String name : stationName) {
            Map<String, Object> map = new HashMap<>();
            if(name.equals("迎宾路点位")){
                params.put("stationId", "B460200002");
            }else  if(name.equals("凤凰路点位")){
                params.put("stationId", "B460200001");
            }
            params.put("stationName", name);
            List<Map<String, Object>> list = baseDao.getCarAirDataMonthChange(params);

            map.put("stationName", name);
            map.put("listData", list);
            resultMapList.add(map);
        }
        return resultMapList;
    }

    @Override
    public Map<String, Object> getAirCarAutoDataRateInfo(Map<String, Object> params) {
        Map<String, Object> resultMap = new HashMap<>(3);
        if(params.get("date")!=null) {
            String date = params.get("date").toString();

            params.put("year", date.split("-")[0]);

            String month = date.split("-")[1];
            if (month.substring(0, 1).equals("0")) {
                month = month.replace("0", "");
            }
            params.put("stationId", params.get("siteName"));



            params.put("month", month);
            resultMap.put("createDate", params.get("date").toString());
            AirCarStatic airCarStatic = airCarAutoDataDao.getAirCarAutoDataRateInfo(params);
            if(airCarStatic == null){
                airCarStatic = new AirCarStatic();
            }


            Map<String, Object> passCountMap =  new HashMap<>();
            passCountMap.put("name","通过");

            passCountMap.put("value",airCarStatic.getPassCount());


            Map<String, Object> noPassCountMap =  new HashMap<>();
            noPassCountMap.put("name","不通过");
            noPassCountMap.put("value",airCarStatic.getNoPassCount());

            List<Map<String, Object>> dataResultMap = new ArrayList<Map<String, Object>>();
            dataResultMap.add(passCountMap);
            dataResultMap.add(noPassCountMap);

            resultMap.put("waterRateNum", dataResultMap);
        }
        return resultMap;
    }

    @Override
    public List<AirCarAutoDataEntity> getEnvAutoCarAirNewData(Map<String, Object> params) {
        return baseDao.getEnvAutoCarAirNewData(params);
    }
}
